package com.zhida.smartcontrol.dialog.jieridialog;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jay.tools.recycler.OnItemClick;
import com.jay.tools.recycler.decoration.RecyclerViewItemDecoration;
import com.jay.tools.ui.ButtonStyle;
import com.jay.tools.utils.DimenUtils;
import com.jay.tools.utils.LogUtil;
import com.jay.tools.utils.ResUtil;
import com.tan.linkpicker.pickerview.builder.TimePickerBuilder;
import com.tan.linkpicker.pickerview.listener.CustomListener;
import com.tan.linkpicker.pickerview.listener.OnTimeSelectChangeListener;
import com.tan.linkpicker.pickerview.view.TimePickerView;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.common.bean.LampBean;
import com.zhida.smartcontrol.mvp.MVPBaseDialogFragment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;

/**
 * author: tanjianpei
 * date: 2019/12/4 004 11:00
 * description: 节日模式，集控和二代开关
 */
public class JieRiDialogFragment extends MVPBaseDialogFragment<JieRiDialogContract.View, JieRiDialogPresenter> implements JieRiDialogContract.View {
    @BindView(R.id.recycler_view)
    RecyclerView mRView;
    @BindView(R.id.btn_auto)
    ButtonStyle mBtnAll;
    @BindViews({R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7})
    List<ButtonStyle> mBtnList;
    @BindViews({R.id.btn_week_1, R.id.btn_week_2, R.id.btn_week_3})
    List<ButtonStyle> mBtnWeekList;
    @BindView(R.id.ll_way)
    LinearLayout mWayView;
    @BindView(R.id.ll_way_1)
    LinearLayout mWayView1;
    @BindView(R.id.ll_week_view_1)
    LinearLayout mWeekView1;
    @BindView(R.id.ll_week_view_2)
    LinearLayout mWeekView2;
    @BindViews({R.id.fl_time_1, R.id.fl_time_1_2, R.id.fl_time_2, R.id.fl_time_2_2, R.id.fl_time_3, R.id.fl_time_3_2})
    List<FrameLayout> mFlList;

    private String[] mTimeArr = new String[]{"", "", "", "", "", ""};
    private List<LampBean> mLampList;
    private JieRiAdapter mAdapter;
    private int mOffset = 0;
    private int mWeekId = -1;
    private List<HolidayChoBean> mHChoList;
    private String mHolidayCho;

    public static JieRiDialogFragment newInstance(String switchType, List<LampBean> data) {
        Bundle args = new Bundle();
        args.putString("type", switchType);
        args.putParcelableArrayList("list", (ArrayList<? extends Parcelable>) data);
        JieRiDialogFragment fragment = new JieRiDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected Object getLayout() {
        return R.layout.dialog_jie_ri;
    }

    @Override
    protected void initDatas(Bundle savedInstanceState) {
        mRView.setLayoutManager(new LinearLayoutManager(mActivity, RecyclerView.HORIZONTAL, false));
        mRView.addItemDecoration(new RecyclerViewItemDecoration.Builder(mActivity)
                .firstLineVisible(true)
                .lastLineVisible(true)
                .thickness(DimenUtils.dp2px(mActivity, 48))
                .color(Color.TRANSPARENT)
                .create());
        mAdapter = new JieRiAdapter(null);
        mAdapter.bindToRecyclerView(mRView);

        mPresenter.getJieRi();

        String switchType = getArguments().getString("type");
        mLampList = getArguments().getParcelableArrayList("list");

        mPresenter.showWayButton(switchType);

        for (int i = 0; i < mFlList.size(); i++) {
            initCustomTimePicker(mFlList.get(i), i);
        }
        for (int i = 0; i < 3; i++) {
            ButtonStyle button = mBtnWeekList.get(i);
            button.setId(i);
            button.setVisibility(View.GONE);
        }
        mWeekView1.setVisibility(View.GONE);
        mWeekView2.setVisibility(View.GONE);
        mPresenter.requestWeekByOffset(mOffset);
    }

    @Override
    public void showButton(int size) {
        if (size == 8) {
            mWayView.setVisibility(View.VISIBLE);
            mWayView1.setVisibility(View.VISIBLE);
        } else if (size == 4) {
            mWayView.setVisibility(View.VISIBLE);
            mWayView1.setVisibility(View.GONE);
        } else {
            mWayView.setVisibility(View.GONE);
            mWayView1.setVisibility(View.GONE);
        }
        for (int i = 0; i < 8; i++) {
            ButtonStyle button = mBtnList.get(i);
            button.setId(i);
            if (i < size) {
                button.setVisibility(View.VISIBLE);
            } else {
                button.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void showWeek(List<HolidayChoBean> beanList) {
        mHChoList = beanList;
        int size = beanList.size();
        if (size == 1) {
            mWeekView1.setVisibility(View.GONE);
            mWeekView2.setVisibility(View.GONE);
        } else {
            mWeekView1.setVisibility(View.VISIBLE);
            mWeekView2.setVisibility(size == 3 ? View.VISIBLE : View.GONE);
            for (int i = 0; i < size; i++) {
                HolidayChoBean bean = beanList.get(i);
                ButtonStyle button = mBtnWeekList.get(i);
                button.setVisibility(View.VISIBLE);
                button.setText(bean.getName());
            }
        }
    }

    @Override
    protected void initEvents(Dialog dialog) {
        mAdapter.setClickListener(new OnItemClick<JieRiBean>() {
            @Override
            public void onClick(JieRiBean info, int tag) {
                mOffset = info.getType();
                for (ButtonStyle buttonStyle : mBtnWeekList) {
                    buttonStyle.setEnabled(true);
                }
                mWeekId = -1;
                if (mOffset == 7) {
                    mHolidayCho = "1";
                    mWeekId = 0;
                }
                mPresenter.requestWeekByOffset(mOffset);
            }
        });
    }

    @OnClick({R.id.btn_cancel, R.id.btn_confirm, R.id.btn_auto})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.btn_auto:
                mBtnAll.setChecked(!mBtnAll.isChecked());
                LogUtil.e(TAG, "选中：" + mBtnAll.isChecked());
                for (ButtonStyle button : mBtnList) {
                    button.setChecked(mBtnAll.isChecked());
                }
                break;
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_confirm:
                mHolidayCho = mWeekId == -1 ? "" : mHChoList.get(mWeekId).getChoose();
                mPresenter.requestSetTime(mLampList, mTimeArr, mBtnList, mOffset, mHolidayCho);
                break;
        }
    }

    @OnClick({R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7})
    public void clickWay(View v) {
        int id = v.getId();
        mBtnAll.setChecked(false);
        mBtnList.get(id).setChecked(!mBtnList.get(id).isChecked());
    }

    @OnClick({R.id.btn_week_1, R.id.btn_week_2, R.id.btn_week_3})
    public void clickWeek(View v) {
        for (ButtonStyle buttonStyle : mBtnWeekList) {
            buttonStyle.setEnabled(true);
        }
        mWeekId = v.getId();
        mBtnWeekList.get(mWeekId).setEnabled(false);
    }

    private void initCustomTimePicker(FrameLayout frameLayout, final int id) {
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();
        selectedDate.set(Calendar.HOUR_OF_DAY, 0);
        selectedDate.set(Calendar.MINUTE, 0);
        selectedDate.set(Calendar.SECOND, 0);
        Calendar startDate = Calendar.getInstance();
        startDate.set(2013, 0, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(3019, 12, 31);

        // 设置默认时间
        mTimeArr[id] = getTime(selectedDate.getTime());

        //时间选择器
        TimePickerView pvTime = new TimePickerBuilder(mActivity, null)
                .setLayoutRes(R.layout.layout_time_picker, new CustomListener() {
                    @Override
                    public void customLayout(View v) {

                    }
                })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                        mTimeArr[id] = getTime(date);
                    }
                })
                .setItemsVisible(3)
                .setType(new boolean[]{false, false, false, true, true, false})
                .setLabel("", "", "", ":", "", "")
                .setContentTextSize(20)
                .setTextColorCenter(ResUtil.getColor(mActivity, R.color.c_3862FF))
                .setDate(selectedDate)
                .setRangDate(startDate, selectedDate)
                .setDividerColor(0x00FFFFFF)
                .setDecorView(frameLayout)
                .build();

        pvTime.setKeyBackCancelable(false);//系统返回键监听屏蔽掉
        pvTime.show();
    }

    private String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(date);
    }

    @Override
    public void showJieRi(List<JieRiBean> jieRiBeans) {
        mAdapter.replaceData(jieRiBeans);
    }


    @Override
    public void onSuccess() {
        dismiss();
    }

}
